package com.example.service.impl;

import com.example.entity.FirstLabel;
import com.example.entity.Label;
import com.example.entity.LabelDTO;
import com.example.entity.SecondLabel;
import com.example.mapper.FirstLabelMapper;
import com.example.mapper.IncomeMapper;
import com.example.mapper.OutcomeMapper;
import com.example.mapper.SecondLabelMapper;
import com.example.service.LabelService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@RequiredArgsConstructor
@Service
public class LabelServiceImpl implements LabelService {
    @Autowired
    private FirstLabelMapper firstLabelMapper;
    @Autowired
    private SecondLabelMapper secondLabelMapper;

    private final IncomeMapper incomeMapper;

    private final OutcomeMapper outcomeMapper;


//    // 删除支出标签 TODO 一次删除一对标签？
//    @Override
//    public void del(LabelDeleteDTO labelDeleteDTO) throws Exception {
//        List<Integer> label1 = labelDeleteDTO.getLabel1();
//        List<Integer> label2 = labelDeleteDTO.getLabel2();
//        if(label2.size() == 0){
//            List<Outcome> outcomes = outcomeMapper.selectList(new LambdaQueryWrapper<Outcome>()
//                    .eq(Outcome::getTypeKey2, label2));
//            if(outcomes != null && outcomes.size() > 0){
//                throw new Exception("存在与该标签关联的收支数据，无法删除");
//            } else if (secondLabelMapper.getByTypeKey(label2)) {
//
//            }
//            firstLabelMapper.del(label1);
//        }else {
//            List<BudgetOutcome> budgetOutcomes = budgetMapper.getByTypeKey2(label2);
//            if(budgetOutcomes != null && budgetOutcomes.size() > 0){
//                throw new Exception("存在与该标签关联的收支数据，无法删除");
//            }
//            secondLabelMapper.del(label2);
//        }
//    }

    @Override
    public void save(LabelDTO labelDTO) {
        String label1 = labelDTO.getLabel1();
        int judge = labelDTO.getJudge();
        String label2 = labelDTO.getLabel2();

        if(label2  == null){
            FirstLabel firstLabel = new FirstLabel();
            firstLabel.setType(label1);
            firstLabel.setJudge(judge);
            firstLabelMapper.save(firstLabel);
        }else{
            Integer id = firstLabelMapper.getByType(label1);
            SecondLabel secondLabel = new SecondLabel();
            secondLabel.setFirstLabelId(id);
            secondLabel.setType(label2);
            secondLabelMapper.save(secondLabel);
        }
    }

    @Override
    public List<Label> list() {
        List<Label> labelList = new ArrayList<>();
        List<FirstLabel> firstLabels = firstLabelMapper.selectAll();
        for (FirstLabel firstLabel : firstLabels) {
            Label label = Label.builder()
                    .id(firstLabel.getId())
                    .type(firstLabel.getType())
                    .judge(firstLabel.getJudge())
                    .secondLabelList(secondLabelMapper.getByTypeKey(firstLabel.getId()))
                    .build();
            labelList.add(label);
        }
        return labelList;
    }
}
